package info.techgeek.labs.collection;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


public class ConcurrencyThreadsTest1 extends Thread {
    char c;
    Hashtable<String, String> map ;
    
    ConcurrencyThreadsTest1(char c ,Hashtable<String, String> map) {
        this.c = c;
        this.map = map;
    }        
    
//    public void run() {
//        for(int i=0; i<100; i++) {
//            System.out.print(c);
//        }
//    }

    public void run() {
    	
            System.out.println(c+" Table Size Before "+map.size());
            //clearTableIterator(map,c);
            //clearTableGenerics1(map);
            clearTableGenerics(map);
            
            System.out.println(c+" Table Size After "+map.size());
            
            /*
            synchronized (map) {
			}
			*/
    }    

    public void clearTableIterator(Hashtable<String, String> map,char c){
		//Set<String> keySet = map.keySet();
		Enumeration<String> enumeration = map.elements();
		System.out.println("Inside Synchronized "+c);
        for (Enumeration e = map.elements() ; e.hasMoreElements() ;) {
            System.out.println(e.nextElement());
            map.remove(e.nextElement());
        }
        
	}
	public void clearTableGenerics(Hashtable<String, String> map){
		
		map.remove("6");
		Set<String> keySet = map.keySet();
		for(String key : keySet){
			System.out.println(key);
//			map.remove(key);
		}
	}
	public void clearTableGenerics1(Hashtable<String, String> map){
		Set<String> keySet = map.keySet();
		List<String> keyList = new ArrayList<String>();
		
		for(String key : keySet){
			keyList.add(key);
		}
		for(String key : keyList){
			map.remove(key);
		}
	}
    
    public static void main(String[] args) {
    	
    	Hashtable<String, String> map = new Hashtable<String, String>();
		map.put("1", "one");
		map.put("2", "two");
		map.put("3", "three");
		map.put("4", "four");
		map.put("5", "five");
		map.put("6", "six");
		
    	ConcurrencyThreadsTest1 bt1 = new ConcurrencyThreadsTest1('1',map);
    	ConcurrencyThreadsTest1 bt2 = new ConcurrencyThreadsTest1('2',map);
    	ConcurrencyThreadsTest1 bt3 = new ConcurrencyThreadsTest1('3',map);
    	ConcurrencyThreadsTest1 bt4 = new ConcurrencyThreadsTest1('4',map);
    	ConcurrencyThreadsTest1 bt5 = new ConcurrencyThreadsTest1('5',map);

	    // start RunBasicThread objects as threads
	    new Thread(bt1).start();
	    new Thread(bt2).start();
	    new Thread(bt3).start();
	    new Thread(bt4).start();
	    new Thread(bt5).start();
	}
}

